// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Fluent;
using Elastic.Clients.Elasticsearch.Serialization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Aggregations;

public sealed partial class MultiTermsAggregation
{
	/// <summary>
	/// <para>
	/// Specifies the strategy for data collection.
	/// </para>
	/// </summary>
	[JsonInclude, JsonPropertyName("collect_mode")]
	public Elastic.Clients.Elasticsearch.Aggregations.TermsAggregationCollectMode? CollectMode { get; set; }

	/// <summary>
	/// <para>
	/// The minimum number of documents in a bucket for it to be returned.
	/// </para>
	/// </summary>
	[JsonInclude, JsonPropertyName("min_doc_count")]
	public long? MinDocCount { get; set; }

	/// <summary>
	/// <para>
	/// Specifies the sort order of the buckets.
	/// Defaults to sorting by descending document count.
	/// </para>
	/// </summary>
	[JsonInclude, JsonPropertyName("order")]
	[SingleOrManyCollectionConverter(typeof(KeyValuePair<Elastic.Clients.Elasticsearch.Field, Elastic.Clients.Elasticsearch.SortOrder>))]
	public ICollection<KeyValuePair<Elastic.Clients.Elasticsearch.Field, Elastic.Clients.Elasticsearch.SortOrder>>? Order { get; set; }

	/// <summary>
	/// <para>
	/// The minimum number of documents in a bucket on each shard for it to be returned.
	/// </para>
	/// </summary>
	[JsonInclude, JsonPropertyName("shard_min_doc_count")]
	public long? ShardMinDocCount { get; set; }

	/// <summary>
	/// <para>
	/// The number of candidate terms produced by each shard.
	/// By default, <c>shard_size</c> will be automatically estimated based on the number of shards and the <c>size</c> parameter.
	/// </para>
	/// </summary>
	[JsonInclude, JsonPropertyName("shard_size")]
	public int? ShardSize { get; set; }

	/// <summary>
	/// <para>
	/// Calculates the doc count error on per term basis.
	/// </para>
	/// </summary>
	[JsonInclude, JsonPropertyName("show_term_doc_count_error")]
	public bool? ShowTermDocCountError { get; set; }

	/// <summary>
	/// <para>
	/// The number of term buckets should be returned out of the overall terms list.
	/// </para>
	/// </summary>
	[JsonInclude, JsonPropertyName("size")]
	public int? Size { get; set; }

	/// <summary>
	/// <para>
	/// The field from which to generate sets of terms.
	/// </para>
	/// </summary>
	[JsonInclude, JsonPropertyName("terms")]
	public ICollection<Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookup> Terms { get; set; }

	public static implicit operator Elastic.Clients.Elasticsearch.Aggregations.Aggregation(MultiTermsAggregation multiTermsAggregation) => Elastic.Clients.Elasticsearch.Aggregations.Aggregation.MultiTerms(multiTermsAggregation);
}

public sealed partial class MultiTermsAggregationDescriptor<TDocument> : SerializableDescriptor<MultiTermsAggregationDescriptor<TDocument>>
{
	internal MultiTermsAggregationDescriptor(Action<MultiTermsAggregationDescriptor<TDocument>> configure) => configure.Invoke(this);

	public MultiTermsAggregationDescriptor() : base()
	{
	}

	private Elastic.Clients.Elasticsearch.Aggregations.TermsAggregationCollectMode? CollectModeValue { get; set; }
	private long? MinDocCountValue { get; set; }
	private ICollection<KeyValuePair<Elastic.Clients.Elasticsearch.Field, Elastic.Clients.Elasticsearch.SortOrder>>? OrderValue { get; set; }
	private long? ShardMinDocCountValue { get; set; }
	private int? ShardSizeValue { get; set; }
	private bool? ShowTermDocCountErrorValue { get; set; }
	private int? SizeValue { get; set; }
	private ICollection<Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookup> TermsValue { get; set; }
	private Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor<TDocument> TermsDescriptor { get; set; }
	private Action<Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor<TDocument>> TermsDescriptorAction { get; set; }
	private Action<Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor<TDocument>>[] TermsDescriptorActions { get; set; }

	/// <summary>
	/// <para>
	/// Specifies the strategy for data collection.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor<TDocument> CollectMode(Elastic.Clients.Elasticsearch.Aggregations.TermsAggregationCollectMode? collectMode)
	{
		CollectModeValue = collectMode;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The minimum number of documents in a bucket for it to be returned.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor<TDocument> MinDocCount(long? minDocCount)
	{
		MinDocCountValue = minDocCount;
		return Self;
	}

	/// <summary>
	/// <para>
	/// Specifies the sort order of the buckets.
	/// Defaults to sorting by descending document count.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor<TDocument> Order(ICollection<KeyValuePair<Elastic.Clients.Elasticsearch.Field, Elastic.Clients.Elasticsearch.SortOrder>>? order)
	{
		OrderValue = order;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The minimum number of documents in a bucket on each shard for it to be returned.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor<TDocument> ShardMinDocCount(long? shardMinDocCount)
	{
		ShardMinDocCountValue = shardMinDocCount;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The number of candidate terms produced by each shard.
	/// By default, <c>shard_size</c> will be automatically estimated based on the number of shards and the <c>size</c> parameter.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor<TDocument> ShardSize(int? shardSize)
	{
		ShardSizeValue = shardSize;
		return Self;
	}

	/// <summary>
	/// <para>
	/// Calculates the doc count error on per term basis.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor<TDocument> ShowTermDocCountError(bool? showTermDocCountError = true)
	{
		ShowTermDocCountErrorValue = showTermDocCountError;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The number of term buckets should be returned out of the overall terms list.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor<TDocument> Size(int? size)
	{
		SizeValue = size;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The field from which to generate sets of terms.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor<TDocument> Terms(ICollection<Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookup> terms)
	{
		TermsDescriptor = null;
		TermsDescriptorAction = null;
		TermsDescriptorActions = null;
		TermsValue = terms;
		return Self;
	}

	public MultiTermsAggregationDescriptor<TDocument> Terms(Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor<TDocument> descriptor)
	{
		TermsValue = null;
		TermsDescriptorAction = null;
		TermsDescriptorActions = null;
		TermsDescriptor = descriptor;
		return Self;
	}

	public MultiTermsAggregationDescriptor<TDocument> Terms(Action<Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor<TDocument>> configure)
	{
		TermsValue = null;
		TermsDescriptor = null;
		TermsDescriptorActions = null;
		TermsDescriptorAction = configure;
		return Self;
	}

	public MultiTermsAggregationDescriptor<TDocument> Terms(params Action<Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor<TDocument>>[] configure)
	{
		TermsValue = null;
		TermsDescriptor = null;
		TermsDescriptorAction = null;
		TermsDescriptorActions = configure;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (CollectModeValue is not null)
		{
			writer.WritePropertyName("collect_mode");
			JsonSerializer.Serialize(writer, CollectModeValue, options);
		}

		if (MinDocCountValue.HasValue)
		{
			writer.WritePropertyName("min_doc_count");
			writer.WriteNumberValue(MinDocCountValue.Value);
		}

		if (OrderValue is not null)
		{
			writer.WritePropertyName("order");
			SingleOrManySerializationHelper.Serialize<KeyValuePair<Elastic.Clients.Elasticsearch.Field, Elastic.Clients.Elasticsearch.SortOrder>>(OrderValue, writer, options);
		}

		if (ShardMinDocCountValue.HasValue)
		{
			writer.WritePropertyName("shard_min_doc_count");
			writer.WriteNumberValue(ShardMinDocCountValue.Value);
		}

		if (ShardSizeValue.HasValue)
		{
			writer.WritePropertyName("shard_size");
			writer.WriteNumberValue(ShardSizeValue.Value);
		}

		if (ShowTermDocCountErrorValue.HasValue)
		{
			writer.WritePropertyName("show_term_doc_count_error");
			writer.WriteBooleanValue(ShowTermDocCountErrorValue.Value);
		}

		if (SizeValue.HasValue)
		{
			writer.WritePropertyName("size");
			writer.WriteNumberValue(SizeValue.Value);
		}

		if (TermsDescriptor is not null)
		{
			writer.WritePropertyName("terms");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, TermsDescriptor, options);
			writer.WriteEndArray();
		}
		else if (TermsDescriptorAction is not null)
		{
			writer.WritePropertyName("terms");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, new Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor<TDocument>(TermsDescriptorAction), options);
			writer.WriteEndArray();
		}
		else if (TermsDescriptorActions is not null)
		{
			writer.WritePropertyName("terms");
			writer.WriteStartArray();
			foreach (var action in TermsDescriptorActions)
			{
				JsonSerializer.Serialize(writer, new Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor<TDocument>(action), options);
			}

			writer.WriteEndArray();
		}
		else
		{
			writer.WritePropertyName("terms");
			JsonSerializer.Serialize(writer, TermsValue, options);
		}

		writer.WriteEndObject();
	}
}

public sealed partial class MultiTermsAggregationDescriptor : SerializableDescriptor<MultiTermsAggregationDescriptor>
{
	internal MultiTermsAggregationDescriptor(Action<MultiTermsAggregationDescriptor> configure) => configure.Invoke(this);

	public MultiTermsAggregationDescriptor() : base()
	{
	}

	private Elastic.Clients.Elasticsearch.Aggregations.TermsAggregationCollectMode? CollectModeValue { get; set; }
	private long? MinDocCountValue { get; set; }
	private ICollection<KeyValuePair<Elastic.Clients.Elasticsearch.Field, Elastic.Clients.Elasticsearch.SortOrder>>? OrderValue { get; set; }
	private long? ShardMinDocCountValue { get; set; }
	private int? ShardSizeValue { get; set; }
	private bool? ShowTermDocCountErrorValue { get; set; }
	private int? SizeValue { get; set; }
	private ICollection<Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookup> TermsValue { get; set; }
	private Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor TermsDescriptor { get; set; }
	private Action<Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor> TermsDescriptorAction { get; set; }
	private Action<Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor>[] TermsDescriptorActions { get; set; }

	/// <summary>
	/// <para>
	/// Specifies the strategy for data collection.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor CollectMode(Elastic.Clients.Elasticsearch.Aggregations.TermsAggregationCollectMode? collectMode)
	{
		CollectModeValue = collectMode;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The minimum number of documents in a bucket for it to be returned.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor MinDocCount(long? minDocCount)
	{
		MinDocCountValue = minDocCount;
		return Self;
	}

	/// <summary>
	/// <para>
	/// Specifies the sort order of the buckets.
	/// Defaults to sorting by descending document count.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor Order(ICollection<KeyValuePair<Elastic.Clients.Elasticsearch.Field, Elastic.Clients.Elasticsearch.SortOrder>>? order)
	{
		OrderValue = order;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The minimum number of documents in a bucket on each shard for it to be returned.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor ShardMinDocCount(long? shardMinDocCount)
	{
		ShardMinDocCountValue = shardMinDocCount;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The number of candidate terms produced by each shard.
	/// By default, <c>shard_size</c> will be automatically estimated based on the number of shards and the <c>size</c> parameter.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor ShardSize(int? shardSize)
	{
		ShardSizeValue = shardSize;
		return Self;
	}

	/// <summary>
	/// <para>
	/// Calculates the doc count error on per term basis.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor ShowTermDocCountError(bool? showTermDocCountError = true)
	{
		ShowTermDocCountErrorValue = showTermDocCountError;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The number of term buckets should be returned out of the overall terms list.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor Size(int? size)
	{
		SizeValue = size;
		return Self;
	}

	/// <summary>
	/// <para>
	/// The field from which to generate sets of terms.
	/// </para>
	/// </summary>
	public MultiTermsAggregationDescriptor Terms(ICollection<Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookup> terms)
	{
		TermsDescriptor = null;
		TermsDescriptorAction = null;
		TermsDescriptorActions = null;
		TermsValue = terms;
		return Self;
	}

	public MultiTermsAggregationDescriptor Terms(Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor descriptor)
	{
		TermsValue = null;
		TermsDescriptorAction = null;
		TermsDescriptorActions = null;
		TermsDescriptor = descriptor;
		return Self;
	}

	public MultiTermsAggregationDescriptor Terms(Action<Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor> configure)
	{
		TermsValue = null;
		TermsDescriptor = null;
		TermsDescriptorActions = null;
		TermsDescriptorAction = configure;
		return Self;
	}

	public MultiTermsAggregationDescriptor Terms(params Action<Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor>[] configure)
	{
		TermsValue = null;
		TermsDescriptor = null;
		TermsDescriptorAction = null;
		TermsDescriptorActions = configure;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (CollectModeValue is not null)
		{
			writer.WritePropertyName("collect_mode");
			JsonSerializer.Serialize(writer, CollectModeValue, options);
		}

		if (MinDocCountValue.HasValue)
		{
			writer.WritePropertyName("min_doc_count");
			writer.WriteNumberValue(MinDocCountValue.Value);
		}

		if (OrderValue is not null)
		{
			writer.WritePropertyName("order");
			SingleOrManySerializationHelper.Serialize<KeyValuePair<Elastic.Clients.Elasticsearch.Field, Elastic.Clients.Elasticsearch.SortOrder>>(OrderValue, writer, options);
		}

		if (ShardMinDocCountValue.HasValue)
		{
			writer.WritePropertyName("shard_min_doc_count");
			writer.WriteNumberValue(ShardMinDocCountValue.Value);
		}

		if (ShardSizeValue.HasValue)
		{
			writer.WritePropertyName("shard_size");
			writer.WriteNumberValue(ShardSizeValue.Value);
		}

		if (ShowTermDocCountErrorValue.HasValue)
		{
			writer.WritePropertyName("show_term_doc_count_error");
			writer.WriteBooleanValue(ShowTermDocCountErrorValue.Value);
		}

		if (SizeValue.HasValue)
		{
			writer.WritePropertyName("size");
			writer.WriteNumberValue(SizeValue.Value);
		}

		if (TermsDescriptor is not null)
		{
			writer.WritePropertyName("terms");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, TermsDescriptor, options);
			writer.WriteEndArray();
		}
		else if (TermsDescriptorAction is not null)
		{
			writer.WritePropertyName("terms");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, new Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor(TermsDescriptorAction), options);
			writer.WriteEndArray();
		}
		else if (TermsDescriptorActions is not null)
		{
			writer.WritePropertyName("terms");
			writer.WriteStartArray();
			foreach (var action in TermsDescriptorActions)
			{
				JsonSerializer.Serialize(writer, new Elastic.Clients.Elasticsearch.Aggregations.MultiTermLookupDescriptor(action), options);
			}

			writer.WriteEndArray();
		}
		else
		{
			writer.WritePropertyName("terms");
			JsonSerializer.Serialize(writer, TermsValue, options);
		}

		writer.WriteEndObject();
	}
}